<?php
class ecAppConfiguration{

  public function  __construct($environment, $debug, $rootDir=null) {

    if(ecEnvironment::getEnvironment() == 'dev')
    {
      self::makeControllers();
      self::makeHelpers();
    }
  }

  public static function exclude($element)
  {
    $ignores = array('.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg');

    foreach($ignores as $value)
    {
      if($value == $element)
      {
        return false;
      }
    }
    return true;
  }

  public static function getApplications()
  {
    $dirName = dirname(__FILE__).'/../../../apps';

    $dirs= array();

    $dir = opendir($dirName);

    while($element = readdir($dir))
    {
      if($element != '.' && $element != '..')
      {
        if (is_dir($dirName.'/'.$element))
        {
          if(self::exclude($element))
          {
            $dirs[] = $element;
          }
        }
      }
    }

    return $dirs;
  }

  public static function getModules()
  {

    $applications = self::getApplications();

    $dirs= array();

    foreach($applications as $application)
    {
      $dirName = dirname(__FILE__).'/../../../apps/'.$application.'/modules';

      $dir = opendir($dirName);

      while($element = readdir($dir))
      {
        if($element != '.' && $element != '..')
        {
          if (is_dir($dirName.'/'.$element))
          {
            if(self::exclude($element))
            {
              $dirs[$application.'/'.$element] = $element;
            }
          }
        }
      }
    }

    return $dirs;
  }

  public static function getControllersModules()
  {
    $files= array();
    $dirs = self::getModules();

    foreach($dirs as $application => $directory)
    {
      $ArrayApplication = explode('/', $application);
      $application = $ArrayApplication[0];

      $dirName = dirname(__FILE__).'/../../../apps/'.$application.'/modules/'.$directory.'/controller';

      $dir = opendir($dirName);

      while($file = readdir($dir))
      {
        if($file != '.' && $file != '..')
        {
          if (!is_dir($dirName.'/'.$file))
          {
            $nameController = basename($file, false === strpos($file, '.controller.php') ? '.php' : '.controller.php');

            $files[$application.'/'.$directory.'/'.$nameController] = $application.'/modules/'.$directory.'/controller/'.$file;
          }
        }
      }
    }

    return $files;
  }

  public static function makeControllers()
  {
    $files = self::getControllersModules();

    $controllers = '';
    foreach ($files as $key => $files)
    {
      $controllers .= sprintf("    '%s' => '%s',\n", strtolower($key), $files);
    }

    $content = preg_replace('/protected \$controllers = array *\(.*?\);/s', sprintf("protected \$controllers = array(\n%s  );", $controllers), file_get_contents(__FILE__));

    file_put_contents(__FILE__, $content);

  }

  // Don't edit this property by hand.
  // To update it, use ecAppConfiguration::makeControllers()

  protected $controllers = array(
    'back/article/test' => 'back/modules/article/controller/test.controller.php',
    'front/article/index' => 'front/modules/article/controller/index.controller.php',
    'front/article/toto' => 'front/modules/article/controller/toto.controller.php',
    'front/test/index' => 'front/modules/test/controller/index.controller.php',
  );


  public function getControllers()
  {
    return $this->controllers;
  }

  public static function makeHelpers()
  {
    ecHelper::setListHelper();
    $files = ecHelper::getListHelper();

    $helpers = '';
    foreach ($files as $key => $files)
    {
      $helpers .= sprintf("    '%s' => '%s',\n", strtolower($key), $files);
    }

    $content = preg_replace('/protected \$helpers = array *\(.*?\);/s', sprintf("protected \$helpers = array(\n%s  );", $helpers), file_get_contents(__FILE__));

    file_put_contents(__FILE__, $content);

  }

  // Don't edit this property by hand.
  // To update it, use ecAppConfiguration::makeHelpers()

  static protected $helpers = array(
    'echelper.class' => '/Users/daviddjian/Sites/eclaire/lib/core/helpers/ecHelper.class.php',
    'i18n' => '/Users/daviddjian/Sites/eclaire/lib/core/helpers/i18n.helper.php',
    'url' => '/Users/daviddjian/Sites/eclaire/lib/core/helpers/url.helper.php',
  );

  static public function getHelpers()
  {
    return self::$helpers;
  }

}